package com.tarena.lbs.coupon.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.coupon.dao.mapper.CouponMapper;
import com.tarena.lbs.coupon.dao.repository.CouponRepository;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.po.CouponPO;
import com.tarena.lbs.coupon.pojo.query.CouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CouponRepositoryImpl implements CouponRepository {
    @Autowired
    private CouponMapper couponMapper;
    @Override
    public void insert(CouponPO couponPO){
        couponMapper.insert(couponPO);
    }
    @Override
    public PageInfo<CouponPO> pagesList(CouponQuery query){
        QueryWrapper<CouponPO> queryWrapper = assembleCouponQuery(query);

        List<CouponPO> list = couponMapper.selectList(queryWrapper);
        return new PageInfo<>(list);
    }
    private QueryWrapper<CouponPO> assembleCouponQuery(CouponQuery query){
        QueryWrapper<CouponPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query.getCouponName())){
            queryWrapper.like("coupon_name", query.getCouponName());
        }
        //3.couponType 优惠券类型 精确查询 where coupon_type = couponType
        if (query.getCouponType() != null) {
            queryWrapper.eq("coupon_type", query.getCouponType());
        }
        //4.status 优惠券状态 精确查询 where status = status
        if (query.getStatus() != null) {
            queryWrapper.eq("status", query.getStatus());
        }
        //5.businessId 商家ID 精确查询 where business_id = businessId
        if (query.getBusinessId() != null) {
            queryWrapper.eq("business_id", query.getBusinessId());
        }
        return queryWrapper;
    }
    @Override
    public void update(CouponPO couponPO){
        couponMapper.updateById(couponPO);
    }
    @Override
    public CouponVO selectById(Integer id){
        CouponPO couponPO = couponMapper.selectById(id);
        CouponVO couponVO = new CouponVO();
        BeanUtils.copyProperties(couponPO, couponVO);
        return couponVO;
    }

    @Override
    public CouponPO getCouponById(Integer couponId) {
        return couponMapper.selectById(couponId);
    }
}
